package com.qh.yyxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qh.yyxt.pojo.entity.meeting.User;
import com.qh.yyxt.pojo.entity.thing.Activity;
import com.qh.yyxt.pojo.vo.ActVO;
import com.qh.yyxt.pojo.vo.ActivityVO;
import com.qh.yyxt.pojo.vo.BringOrBackVO;
import com.qh.yyxt.service.ActivityService;
import com.qh.yyxt.service.ThingSizeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class LendThingController {

    @Autowired
    private ThingSizeService thingSizeService;

    @Autowired
    private ActivityService activityService;



    /**
     *  借用物品
     */
    @PostMapping("/user/borrowThing")
    @Transactional
    public String addApply(//@RequestBody
             ActivityVO activityVO,
            @RequestParam("tid[]") String[] tid,
            @RequestParam("sid[]") String[] sid,
            @RequestParam("num[]") String[] num){

        if(activityVO.getStartTime().isAfter(activityVO.getEndTime())){
            return "请填写正确的借用时间！";
        }
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        activityVO.setUid(user.getId());
        int[] tId = Arrays.stream(tid).mapToInt(Integer::parseInt).toArray();
        int sId[] = Arrays.stream(sid).mapToInt(Integer::parseInt).toArray();
        int nums[] = Arrays.stream(num).mapToInt(Integer::parseInt).toArray();
        //先判断数量是不是都小于数据库的数量
        for (int i = 0; i < sId.length; i++) {
            if (!thingSizeService.judgeNum(sId[i], nums[i])) {
                return "数量超出最大限制！";
            }
        }
        return activityService.insertActivity(activityVO,tId,sId,nums);
//        if ( {
//            return "";
//        }else {
//            return "申请失败！库存不足或信息错误！";
//        }
    }
    /**
     * 审批列表
     */
    @GetMapping("/user/actList")
    public List<Activity> actList(){
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("apr_status","待审批");
        return activityService.list(wrapper);
    }
    /**
     * 个人审批列表
     */
    @GetMapping("/user/actOne")
    public List<Activity> oneActList(){
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",user.getId());
        return activityService.list(wrapper);
    }
    /**
     * 查看详情
     */
    @GetMapping("/user/getActById")
    public ActVO getActById(Long id){
        return activityService.getActById(id);
    }
    /**
     * 进行审批
     */
    @PostMapping("/admin/doBorrowThingApply")//审批请求(0 拒绝 1 通过)
    public String doBorrowThingApply(@RequestParam("aid") Long aid,
                                      @RequestParam("approver")String approver,
                                      @RequestParam("mark")String mark){
        System.out.println(aid+" "+approver+" "+mark);
        String status = "未通过";
        if (mark.equals("0")){
            boolean b = activityService.setAprStatus(aid, approver, status);
            if (b) return "操作成功！";
            else return "操作异常，请稍后重试";
        }else {
            status = "审批通过";
            boolean b = activityService.approval(aid, approver, status);
            if(b){
                return "操作成功！";
            }else {
                return "库存不足或操作异常！";
            }
        }
    }
    /**
     * 历史记录
     */
    @GetMapping("/user/borrowThingHistory")
    public List<Activity> actListForHistory(){
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("apr_status","审批通过")
                .or()
                .eq("apr_status","未通过");
        return activityService.list(wrapper);
    }
    /**
     * 活动展示
     */
    @GetMapping("/user/getAct")
    public List<Activity> getAct(){
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("apr_status","审批通过");
        return activityService.list(wrapper);
    }
    /**
     * 出库操作
     */
    @PostMapping("/user/outStock")
    public String outStock( BringOrBackVO bringOrBackVO){
        if(bringOrBackVO ==null) return "提交数据有误！";
        return activityService.outStock(bringOrBackVO);
    }
    /**
     * 入库操作
     */
    @PostMapping("/user/inStock")
    public String inStock(@RequestBody BringOrBackVO bringOrBackVO){
//        System.out.println(bringOrBackVO);
        if(bringOrBackVO ==null) return "提交数据有误！";
        return activityService.inStock(bringOrBackVO);
    }
    /**
     * 取消活动
     */
    @RequestMapping("/user/cancelAct")
    public String cancelAct(Long id){
        Activity act = activityService.getById(id);
        act.setLendStatus("已取消");
        boolean b = activityService.updateById(act);
        if(b) return "取消成功";
        else return "取消失败";
    }


}
